package com.example.jmm.myndk;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by jmm on 17-6-7.
 */

public final class USBMonitor {

    private static final String TAG = "TAG";
    //定义广播标识
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    //注册接收广播 获取到usb 权限时系统广播

    private final WeakReference<Context> mWeakContext;
    private final UsbManager mUsbManager;
    private PendingIntent mPermissionIntent = null;
    private final OnDeviceConnectListener mOnDeviceConnectListener;
    //private volatile boolean destroyed;

    public interface OnDeviceConnectListener {

        public void onAttach(UsbDevice device);

        public void onConnect(UsbDevice device);

    }

    public USBMonitor(final Context context, final OnDeviceConnectListener listener) {
        mOnDeviceConnectListener = listener;
        mWeakContext = new WeakReference<Context>(context);
        mUsbManager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
    }

    public synchronized void register(){
        //绑定广播
        final Context context = mWeakContext.get();
        if(context != null) {
            mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            //注册接收广播
            context.registerReceiver(mUsbReceiver, filter);
        }
    }

    public Iterator<UsbDevice> getDeviceList() {
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        return deviceList.values().iterator();
    }

    public synchronized boolean requestPermission(final UsbDevice device) {
        boolean result = true;
        try {
            mUsbManager.requestPermission(device, mPermissionIntent);
        } catch (final Exception e) {
            result = false;
        }
        return result;
    }

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(device != null){
                            //成功
                            mOnDeviceConnectListener.onConnect(device);
                        }
                    }
                    else {
                        Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }
        }

    };
}
